package ${filePackageName};

import cn.hutool.core.collection.ListUtil;
import ${utilsPackage}.ConstantUtil;
import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.*;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Swagger注册
 * @author ${projectAuthor}
 * @version 1.0
 * @date ${nowDate}
 */
@Configuration
@EnableSwagger2WebMvc
public class Swagger2Config {
    public static String SYS_NAME = "${projectSysName}";
    public static String DEFAULT_VERSION = "V1.0.0";

    @Resource
    private OpenApiExtensionResolver openApiExtensionResolver;

    private ApiInfo apiInfo(String sysName,String version) {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title(sysName+"服务")
                .description(sysName+"服务接口文档")
                .termsOfServiceUrl("http://127.0.0.1:${projectPort}/")
                .contact(new Contact("${projectAuthor}", "http://127.0.0.1:${projectPort}/doc.html","${authorEmail}"))
                .version(version).build();
        return apiInfo;
    }

    @Bean
    public Docket createAPI() {
        return new Docket(DocumentationType.SWAGGER_2)
                .host("127.0.0.1:${projectPort}")
                .apiInfo(apiInfo(SYS_NAME,DEFAULT_VERSION))
                .pathMapping("/")
                .groupName("Web端API文档"+DEFAULT_VERSION)
                .select()
                .apis(RequestHandlerSelectors.basePackage("${controllerPackage}"))
                .paths(PathSelectors.any())
                .build()
                .extensions(openApiExtensionResolver.buildSettingExtensions())
                .globalOperationParameters(getParameters())
                .globalResponseMessage(RequestMethod.POST,getResponseMessages())
                .globalResponseMessage(RequestMethod.PUT,getResponseMessages())
                .globalResponseMessage(RequestMethod.GET,getResponseMessages())
                .globalResponseMessage(RequestMethod.DELETE,getResponseMessages())
                .globalResponseMessage(RequestMethod.OPTIONS,getResponseMessages())
                .globalResponseMessage(RequestMethod.PATCH,getResponseMessages())
                .globalResponseMessage(RequestMethod.HEAD,getResponseMessages())
                .globalResponseMessage(RequestMethod.TRACE,getResponseMessages())
                ;
    }


    private static List<Parameter> getParameters(){
        List<Parameter> pars = new ArrayList<Parameter>();
        pars.add(new ParameterBuilder().name(ConstantUtil.DEFAULT_AUTH_TOKEN)
                .description("用户验证Token")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(true).build());

        List<String> langList = ListUtil.toList(ConstantUtil.LANGUAGES);
        AllowableListValues allowableListValues = new AllowableListValues(langList,"String");
        pars.add(new ParameterBuilder().name(ConstantUtil.HEADER_PARAM_LANGUAGE)
                .scalarExample("zh_CN")
                .allowableValues(allowableListValues)
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false).build());
        return pars;
    }

    private static List<ResponseMessage> getResponseMessages(){
        List<ResponseMessage> responseMessages = new ArrayList<>();
        ResponseMessage responseMessage = new ResponseMessageBuilder().code(200).message("请求成功").build();
        ResponseMessage responseMessage2 = new ResponseMessageBuilder().code(401).message("您的登录信息已过期，请重新登录").build();
        ResponseMessage responseMessage3 = new ResponseMessageBuilder().code(402).message("您的账号已在其他设备登录").build();
        ResponseMessage responseMessage4 = new ResponseMessageBuilder().code(403).message("没有访问权限").build();
        ResponseMessage responseMessage5 = new ResponseMessageBuilder().code(404).message("未查到数据").build();
        ResponseMessage responseMessage6 = new ResponseMessageBuilder().code(500).message("服务器内部异常").build();
        responseMessages.add(responseMessage);
        responseMessages.add(responseMessage2);
        responseMessages.add(responseMessage3);
        responseMessages.add(responseMessage4);
        responseMessages.add(responseMessage5);
        responseMessages.add(responseMessage6);
        return responseMessages;
    }
}